import {Injectable} from '@angular/core';
import {AliasOptions, JPush} from '@jiguang-ionic/jpush';
import {PushService} from './push.service';
import {PushMsgModel} from '../../../model/services/native/push/push-msg-model';
import {Platform} from 'ionic-angular';

@Injectable()
export class JPushService extends PushService {

    private sequence: number = 0;

    private hasRegister = false;

    constructor(private jPush: JPush,
                private platform: Platform) {
        super();
    }

    public init() {
        if (!this.platform.is("cordova")) return;
        //极光推送init方法回调无效，此处坑爹
        this.jPush.init();
    }

    public setAlias(alias: string, successCallback?: ((value: String) => void), errorCallback?: ((error: any) => void)) {
        if (!this.platform.is("cordova")) {
            this.functonCall(successCallback, alias);
            return;
        }
        this.jPush.setAlias(new Alias(this.sequence++, alias)).then((data) => this.functonCall(successCallback, data.alias)).catch(errorCallback);
    }

    public deleteAlias(alias: string, successCallback?: ((value: boolean) => void), errorCallback?: ((error: any) => void)) {
        if (!this.platform.is("cordova")) {
            this.functonCall(successCallback, true);
            return;
        }
        this.jPush.deleteAlias(new Alias(this.sequence++, alias)).then((data) => this.functonCall(successCallback, true)).catch(errorCallback);
    }

    public getAlias(successCallback?: ((value: String) => void), errorCallback?: ((error: any) => void)) {
        if (!this.platform.is("cordova")) {
            this.functonCall(successCallback, "非Cordova");
            return;
        }
        this.jPush.getAlias(new Alias(this.sequence++)).then((data) => this.functonCall(successCallback, data.alias)).catch(errorCallback);
    }

    public openNotification(onOpenNotification: ((data: PushMsgModel) => void)) {
        if (!this.platform.is("cordova") || this.hasRegister) {
            return;
        }

        document.addEventListener('jpush.openNotification', (event: any) => {
            let msg: PushMsgModel = new PushMsgModel();
            if (this.platform.is("android")) {
                msg.content = event.alert;
                msg.msgid = event.extras["cn.jpush.android.MSG_ID"];
                msg.msgtype = parseInt(event.extras.msgtype);
                msg.extras = event.extras;
            } else {  // iOS
                if (event.aps == undefined) { // 本地通知
                    msg.content = event.content;
                    msg.msgid = event.extras["cn.jpush.ios.MSG_ID"];
                    msg.msgtype = parseInt(event.extras.msgtype);
                    msg.extras = event.extras;
                } else {  // APNS
                    msg.content = event.aps.alert;
                    msg.badge = event.aps.badge;
                    msg.msgid = event._j_msgid;
                    msg.msgtype = parseInt(event.msgtype);
                    msg.extras = event;
                }
            }
            onOpenNotification.call(this, msg);
        }, false);

        this.hasRegister = true;
    }

    private functonCall(func: Function, ...argArray: any[]) {
        if (func) {
            func.call(this, argArray);
        }
    }

}

export class Alias implements AliasOptions {
    sequence: number;
    alias?: string;

    constructor(sequence: number, alias?: string) {
        this.alias = alias;
        this.sequence = sequence;
    }
}
